import com.softwarecraftsmen.orogeny.BaseDirectory;
import com.softwarecraftsmen.orogeny.BuildScript;
import com.softwarecraftsmen.orogeny.TasksExecuteInParallel;
import com.softwarecraftsmen.orogeny.UpperCaseEnvironmentVariableOnWindows;
import com.softwarecraftsmen.orogeny.filing.AbsoluteDirectory;
import com.softwarecraftsmen.orogeny.filing.AbsoluteFile;
import com.softwarecraftsmen.orogeny.filing.findFileFilters.FindFilesFilter;

import static com.softwarecraftsmen.orogeny.actions.CopyFilesAction.flatHardLinkFiles;
import static com.softwarecraftsmen.orogeny.actions.DeleteDirectoryAction.deleteDirectory;
import static com.softwarecraftsmen.orogeny.actions.ExecuteAction.FifteenSeconds;
import static com.softwarecraftsmen.orogeny.actions.ExecuteAction.execute;
import static com.softwarecraftsmen.orogeny.actions.JarTogetherAction.jarTogether;
import static com.softwarecraftsmen.orogeny.actions.MakeDirectoryAction.makeDirectory;
import static com.softwarecraftsmen.orogeny.actions.ModuleAction.module;
import static com.softwarecraftsmen.orogeny.actions.ZipTogetherAction.zipTogether;
import static com.softwarecraftsmen.orogeny.actions.execute.OperatingSystemAndPathDependentExecutable.operatingSystemAndPathDependentExecutable;
import static com.softwarecraftsmen.orogeny.filing.FilesFilteredAbsolutePaths.filesFilteredAbsolutePaths;
import static com.softwarecraftsmen.orogeny.filing.findFileFilters.AbstractFindFilesFilter.*;
import static com.softwarecraftsmen.orogeny.filing.findFileFilters.FileHasExtensionFindFilesFilter.fileHasExtension;

/**
 * This file is Copyright © 2008 Software Craftsmen Limited. All Rights Reserved.
 */

@BaseDirectory("../..")
@UpperCaseEnvironmentVariableOnWindows({"PATH"})
@TasksExecuteInParallel(false)
public class OrogenyBuildScript extends BuildScript
{{
	final AbsoluteDirectory output = inBuildDirectory("build");
	final AbsoluteDirectory library = inBuildDirectory("library");
	final AbsoluteDirectory source = inBuildDirectory("source");

	final AbsoluteFile annotations = library.subDirectory("IntelliJ", "7").file("annotations.jar");
	final AbsoluteFile joptSimple = library.subDirectory("jopt-simple", "4.5").file("jopt-simple-4.5.jar");

	task("clean").does
	(
		deleteDirectory(output)
	);

	task("generate keys").does
	(
		execute(operatingSystemAndPathDependentExecutable("keytool")).inWorkingDirectory(inBuildDirectory()).forUpTo(FifteenSeconds).
		withArgument("-genkey").
		withArguments
		(
			"-alias", "Raph",
			"-keypass", "password",
			"-dname", "CN=Raphael James Cohn, OU=Development, O=Software Craftsmen Limited, L=London, S=London, C=GB",
			"-keyalg", "RSA",
			"-keysize", "1024",
			"-storepass", "storepass"
		).
		withEnvironmentVariable("PATH", currentOperatingSystemFamily().getEnvironmentVariableWithCaseSensitivityPerPlatform("PATH"))
	);

	task("compile operatingSystems").dependsOn("clean").does
	(
		module("operatingSystems").compile(source.subDirectory("operatingSystems")).usingClassPath(annotations).toOutputDirectory(output).withoutMainClass()
	);

	task("compile base64").dependsOn("clean").does
	(
		module("base64").compile(source.subDirectory("base64")).usingClassPath(annotations).toOutputDirectory(output).withoutMainClass()
		//,archiveAs(TarArchive).to(output.file("base64.tar")).roots(output.subDirectory("base64", "classes")).capturing(AllFiles)
	);

	task("compile inputStreamReaders").dependsOn("compile base64").does
	(
		module("inputStreamReaders").compile(source.subDirectory("inputStreamReaders")).usingClassPath(registeredPaths("base64")).toOutputDirectory(output).withoutMainClass()
	);

	task("compile outputStreamWriters").dependsOn("clean").does
	(
		module("outputStreamWriters").compile(source.subDirectory("outputStreamWriters")).usingClassPath(annotations).toOutputDirectory(output).withoutMainClass()
	);

	task("compile filing").dependsOn("compile operatingSystems", "compile inputStreamReaders", "compile outputStreamWriters").does
	(
		module("filing").compile(source.subDirectory("filing")).usingClassPath(registeredPaths("operatingSystems", "inputStreamReaders", "outputStreamWriters")).toOutputDirectory(output).withoutMainClass()
	);

	task("compile collections").dependsOn("clean").does
	(
		module("collections").compile(source.subDirectory("collections")).usingClassPath(annotations).toOutputDirectory(output).withoutMainClass()
	);

	task("compile archivers").dependsOn("compile filing").does
	(
		module("archivers").compile(source.subDirectory("archivers")).usingClassPath(registeredPaths("filing")).toOutputDirectory(output).withoutMainClass()
	);

	// base64 is zip signing, which probably ought to go in a little sub module
	task("compile orogeny").dependsOn("compile operatingSystems", "compile filing", "compile base64", "compile archivers", "compile collections").does
	(
		module("orogeny").compile(source.subDirectory("orogeny")).usingClassPath(registeredPaths("operatingSystems", "filing", "base64", "archivers", "collections")).toOutputDirectory(output).withoutMainClass()
	);

	task("compile application").dependsOn("compile orogeny").does
	(
		module("application").compile(source.subDirectory("application")).usingClassPath(registeredPaths("orogeny")).toOutputDirectory(output).withoutMainClass()
	);

	final String orogenyMainClass = "com.softwarecraftsmen.orogeny.executable.ConsoleEntryPoint";
	task("compile executable").dependsOn
	(
		"compile application"
	).does
	(
		module("executable").compile(source.subDirectory("executable")).usingClassPath(joptSimple.and(registeredPaths("application"))).toOutputDirectory(output).withMainClass(orogenyMainClass)
	);

	final AbsoluteDirectory distribution = output.subDirectory("distribution");
	final AbsoluteFile orogenyWithDependenciesJar = distribution.file("orogeny-with-dependencies.jar");
	final AbsoluteFile orogenyWithoutDependenciesJar = distribution.file("orogeny-without-dependencies.jar");
	final AbsoluteFile orogenySourcesZip = distribution.file("orogeny.sources.zip");
	final FindFilesFilter isInLibrary = isInRoot(library);
	task("compile").dependsOn("compile executable").does
	(
		makeDirectory(distribution),
		flatHardLinkFiles(isInLibrary.and(ZipOrJar)).from(registeredPaths("executable")).to(distribution),
		jarTogether(registeredPaths("executable")).capturing(Jar).to(orogenyWithDependenciesJar).withoutClassPath().withMainClass(orogenyMainClass),
		jarTogether(registeredPaths("executable")).capturing(Jar.exclude(isInLibrary)).to(orogenyWithoutDependenciesJar).withClassPath(filesFilteredAbsolutePaths(registeredPaths("executable"), isInLibrary)).withMainClass(orogenyMainClass),
		zipTogether(output).capturing(fileHasExtension("source.zip")).to(orogenySourcesZip)
		//signJars(orogenyWithDependenciesJar).using(key("Raph").password("password")).in(UserKeyStore.password("storepass"))
	);
}

static
{
	configure();
}
}
